package lc.oa.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import org.hibernate.testing.util.ReflectionUtil;

import java.util.List;

import antlr.StringUtils;

/**
 * @Description: 下拉选择适配器
 * @Author: lucifer
 * @CreateDate: 2020/8/13 10:18
 * @UpdateUser:
 * @UpdateDate:
 * @UpdateRemark:
 * @Version: 1.0
 */
public class SpinAdapter<T> extends ArrayAdapter {
    // Your sent context
    private Context context;
    // Your custom values for the spinner (User)
    private List<T> values;
    private String displayText;
   // private String vals;
    public SpinAdapter(Context context, int textViewResourceId,
                       List<T> values,String _displayText) {
        //T dTxt =ReflectionUtil.getFieldValue(value,displayText);
        super(context, textViewResourceId, values);
//        if (_displayText==null || _displayText.equals("")){
//            throw new RuntimeException("_displayText不能为空!");
//        }
        this.context = context;
        this.values = values;
        this.displayText=_displayText;
       // this.vals=_val;
    }
    private int selectedPostion;

    public void setSelectedPostion(int selectedPostion) {
        this.selectedPostion = selectedPostion;
    }

    @Override
    public int getCount(){
        return values.size();
    }

    @Override
    public T getItem(int position){
        return values.get(position);
    }

    @Override
    public long getItemId(int position){
        return position;
    }


    // And the "magic" goes here
    // This is for the "passive" state of the spinner
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // I created a dynamic TextView here, but you can reference your own  custom layout for each spinner item
        TextView label = (TextView) super.getView(position, convertView, parent);
        //label.setTextColor(Color.BLACK);
        // Then you can get the current item using the values array (Users array) and the current position
        // You can NOW reference each method you has created in your bean object (User class)
        //label.setText(values[position].getName());

//        T value=values.get(position);
//        T dTxt =ReflectionUtil.getFieldValue(value,displayText);
//        label.setText(dTxt.toString());
        if(selectedPostion == position){
            label.setTextColor(0xff373741);
            label.getPaint().setFakeBoldText(true);
        }
        else{
            label.setTextColor(0xff6d6d6d);
            label.getPaint().setFakeBoldText(false);
        }
        // And finally return your dynamic (or custom) view for each spinner item
        return label;
    }

    // And here is when the "chooser" is popped up
    // Normally is the same view, but you can customize it if you want
    @Override
    public View getDropDownView(int position, View convertView,
                                ViewGroup parent) {
        TextView label = (TextView) super.getDropDownView(position, convertView, parent);
        label.setTextColor(Color.BLACK);
//        T value=values.get(position);
//       T dTxt =ReflectionUtil.getFieldValue(value,displayText);
//        label.setText(dTxt.toString());

        return label;
    }

}
